package com.tulingxueyuan.mall.modules.oms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tulingxueyuan.mall.dto.OmsOrderViewDTO;
import com.tulingxueyuan.mall.dto.OrderConditionDTO;
import com.tulingxueyuan.mall.dto.OrderReceiverInfoDTO;
import com.tulingxueyuan.mall.dto.OrderUpdateStatusDTO;
import com.tulingxueyuan.mall.modules.oms.mapper.OmsOrderMapper;
import com.tulingxueyuan.mall.modules.oms.model.OmsOrder;
import com.tulingxueyuan.mall.modules.oms.service.OmsOrderService;
import io.netty.util.internal.StringUtil;
import org.apache.ibatis.annotations.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author XuShu
 * @since 2023-05-28
 */
@Service
public class OmsOrderServiceImpl extends ServiceImpl<OmsOrderMapper, OmsOrder> implements OmsOrderService {
    @Resource
    private OmsOrderMapper omsOrderMapper;
    /**
     * 订单条件分页
     * @param orderConditionDTO
     * @return
     */
    @Override
    public Page listPage(OrderConditionDTO orderConditionDTO) {
        Page<OmsOrder> pageInfo = new Page<>(orderConditionDTO.getPageNum(), orderConditionDTO.getPageSize());
        LambdaQueryWrapper<OmsOrder> queryWrapper = new LambdaQueryWrapper<>();
//        订单单号
        if (orderConditionDTO.getOrderSn() != null){
            queryWrapper.eq(OmsOrder::getOrderSn,orderConditionDTO.getOrderSn());
        }
        //        下单人
        if (!StringUtils.isEmpty(orderConditionDTO.getReceiverKeyword())){
            queryWrapper.eq(OmsOrder::getReceiverName,orderConditionDTO.getReceiverKeyword());
        }
        //        下单时间
        if (orderConditionDTO.getCreateTime() != null){
            queryWrapper.eq(OmsOrder::getCreateTime,orderConditionDTO.getCreateTime());
        }
        //        订单状态
        if (orderConditionDTO.getOrderType() != null){
            queryWrapper.eq(OmsOrder::getOrderType,orderConditionDTO.getOrderType());
        }
        //        订单分类
        if (orderConditionDTO.getOrderType() != null){
            queryWrapper.eq(OmsOrder::getOrderType,orderConditionDTO.getOrderType());
        }
        //        来源
        if (orderConditionDTO.getSourceType() != null){
            queryWrapper.eq(OmsOrder::getSourceType,orderConditionDTO.getSourceType());
        }
        this.page(pageInfo,queryWrapper);
        return pageInfo;
    }

    /**
     * 订单发货
     * @param list
     * @return
     */
    @Override
    public Boolean updateShipment(List<OrderUpdateStatusDTO> list) {
        List<OmsOrder> omsOrderList = new ArrayList<>();
//        遍历取值 并加到集合中
        for (OrderUpdateStatusDTO orderUpdateStatusDTO : list) {
            OmsOrder omsOrder = new OmsOrder();
            omsOrder.setId(orderUpdateStatusDTO.getOrderId());
            omsOrder.setDeliverySn(orderUpdateStatusDTO.getDeliverySn());
            omsOrder.setReceiverName(orderUpdateStatusDTO.getDeliveryCompany());
            omsOrder.setStatus(2);

            omsOrderList.add(omsOrder);
        }
        return this.updateBatchById(omsOrderList);
    }

    /**
     * 修改联系人信息
     * @param orderReceiverInfoDTO
     * @return
     */
    @Override
    public Boolean updateSReceiver(OrderReceiverInfoDTO orderReceiverInfoDTO) {
        OmsOrder omsOrder = new OmsOrder();
        BeanUtils.copyProperties(orderReceiverInfoDTO,omsOrder);
        omsOrder.setId(orderReceiverInfoDTO.getOrderId());
        return this.updateById(omsOrder);
    }

    /**
     * 修改备注
     * @param omsOrder
     * @return
     */
    @Override
    public Boolean updateNote(OmsOrder omsOrder) {
        return this.updateById(omsOrder);
    }

    /**
     * 取消订单
     * @param ids 订单多个id
     * @param note 取消订单备注
     * @return
     */
    @Override
    public Boolean closeOrder(List<Long> ids, String note) {
        List<OmsOrder> list = new ArrayList<>();
        for (Long id : ids) {
            OmsOrder omsOrder = new OmsOrder();
            omsOrder.setId(id);
            omsOrder.setNote(note);
            omsOrder.setStatus(4);

            list.add(omsOrder);
        }
        return this.updateBatchById(list);
    }

    /**
     * 查询订单的详情
     * @param id
     * @return
     */
    @Override
    public OmsOrderViewDTO getByIdWithOrderItem(Integer id) {
        OmsOrderViewDTO omsOrderViewDTO = omsOrderMapper.getByIdWithOrderItem(id);
        return omsOrderViewDTO;
    }
}
